from time import ctime
from numpy import dot, array
logFile = open('log.txt','a')
analysisf = open('debug.txt', 'a')



class Node():
	nodeId = None	# node id which is used for reference
	baseClf = None	# basic classifier which is the nb classifier here
	featureLength = 2	# number of features we are using
	weight = None	# the weight used to give actual score
	children = []	# ids of children nodes
	parents = []
	featureExtractor = None # extracts features from give data

	pendingProducts = []

	def predict(self, data):	# this is actually a binary classifier
									# but we use its score for internode 
									# comparison
		return dot(featureExtractor(data), weight)	# returns score

	def train(self, data, isIn):	#update weight according to hinge loss
		if predict(weight, data)*isIn>1:
			return
		weight+=isIn*features

	def __init__(self, featureNum, nodeID, childs, parents, baseCLF=None):
		self.featureLength = featureNum
		self.nodeId = nodeID
		self.baseClf = baseCLF
		self.children = childs
		self.parents = parents
		self.weight = array([0.0]*self.featureLength)

class Path():
	path = [0]
	prob = 1.0

class ResultM():
	clf_name = ''
	path = ''
	probMatrix = None
	typeName = 'ResultM'

	def getResultM(self, p, data, Her_tree):
		'''
			build the result matrix on node p, returns the classes array 
		'''
		raise 'To be implemented'



def printLog(f,s):
	print s
	print >>f, s
	f.flush()

def analysisLog(s):
	print s
	print >>analysisf, s
	analysisf.flush()

def NumPathMatched(predicted,labels):
	maxCnt = 0
	for l in labels:
		cnt = 0
		for i in range(min(len(predicted),len(l))):
			if predicted[i] == l[i]:
				cnt += 1
			else:
				break
		if cnt == len(l):
			return 9
		if cnt > maxCnt:
			maxCnt = cnt

	return maxCnt

def resultLog(predicted, labels):
	correct = [0] * 10
	printLog(logFile, 'L1 to L9 in %')
	for i in range(len(predicted)):
		ret = NumPathMatched(predicted[i][1:],labels[i])
		for l in range(0,ret+1):
			correct[l]+=1

	for i in range(1,10):
		printLog(logFile, str(round(float(correct[i])/correct[0]*100, 2)))
	printLog(logFile, '--------------')